package com.lkm.market2_0.m_imple.steelyardstatus;

import android.os.Handler;
import android.os.Looper;

import com.lkm.frame.P;
import com.lkm.frame.otto.LinkBus;
import com.lkm.frame.task2.Task;
import com.lkm.javatool.StringHelp;
import com.lkm.market2_0.ExpandConfig;
import com.lkm.market2_0.entity.ResponEntity;
import com.lkm.market2_0.m.steelyarddata.SteelyardDataModule;
import com.lkm.market2_0.m.steelyardstatus.StatusInfo;
import com.lkm.market2_0.m.steelyardstatus.SteelyardStatusManagerModule;
import com.lkm.market2_0.m_imple.steelyardserver.server.Config;
import com.lkm.market2_0.m.steelyardserver.event.OnSteelardOnlineEvent;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;




 class SteelyardStatusModuleImple implements SteelyardStatusManagerModule {


    private SteelyardDataModule mSteelyardDataModule;


    private Map<String,StatusInfo> deviceIdStatusInfoMap=new ConcurrentHashMap<>();

    public SteelyardStatusModuleImple(SteelyardDataModule steelyardDataModule) {
        this.mSteelyardDataModule=steelyardDataModule;
        init();
    }

    @Override
    public void receiver(String steelyardMac, long time, boolean isException,String version,String steelyardNumno,float voltage) {
        if(StringHelp.isEmpty(steelyardMac))return;

        StatusInfo statusInfo = deviceIdStatusInfoMap.get(steelyardMac);

        if(statusInfo!=null){

        }

        if(P.isDebug()&&false) {
            P.p("=======receiver 台秤有数据上来" + steelyardMac);
        }

        
        if(statusInfo==null){
            statusInfo=new StatusInfo();
            statusInfo.setDisConnect(false);
            statusInfo.setSteelyardMac(steelyardMac);
            deviceIdStatusInfoMap.put(steelyardMac,statusInfo);
            onSteelyardLine(steelyardMac,true);
        }else{


            if(!statusInfo.isOnline()){
                statusInfo.setDisConnect(false);
                onSteelyardLine(steelyardMac,false);
                mSteelyardDataModule.clearSaleCacheInfo(steelyardMac);
            }
            
            if(!statusInfo.isOnline()||mSteelyardDataModule.getSteelyardInfo(steelyardMac)==null){
                
                mSteelyardDataModule.LoadSteelyardInfoForS(steelyardMac);
            }
        }
        statusInfo.setLastActionTime(time);
        statusInfo.setIsException(isException);
        
        if(StringHelp.isEmpty(version)) {
            version="";
        }
        if(StringHelp.isEmpty(steelyardNumno)) {
            steelyardNumno="";
        }
        statusInfo.setVersion(version);
        statusInfo.setNumno(steelyardNumno);
        statusInfo.setVoltage(voltage);


    }



    private void onSteelyardLine(String steelyardMac,boolean isFirst){
        if(ExpandConfig.instance.isUI()){
            LinkBus.sendBroadcast(new OnSteelardOnlineEvent(steelyardMac));
        }

    }

    public StatusInfo[] getStatusInfos(){
        return deviceIdStatusInfoMap.values().toArray(new StatusInfo[deviceIdStatusInfoMap.size()]);
    }

    @Override
    public StatusInfo getStatusInfoBySteelyardMac(String steelyardMac) {
        return deviceIdStatusInfoMap.get(steelyardMac);
    }

    @Override
    public StatusInfo getStatusInfoWrite(String steelyardMac) {
        return deviceIdStatusInfoMap.get(steelyardMac);
    }

    @Override
    public Task<Void, Void, ResponEntity<Void>> upStatusToServer() {
        return UpStatusTask.up(SteelyardStatusModuleImple.this);
    }

    private Handler handler;
    private void init(){
        handler=new Handler(Looper.myLooper());
    }
    @Override
    public void startTimingUp() {
        handler.removeCallbacksAndMessages(null);
        loopUp();
    }
    private void loopUp(){
        
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {

                if(!UpStatusTask.isUpdateIng()){
                    UpStatusTask.up(SteelyardStatusModuleImple.this);
                }

                loopUp();
            }
        },Config.upSteelyardStatusTime);
    }
}
